//
//  GJFEditNameViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/11.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFEditNameViewController.h"
#import "GJFTextField.h"

@interface GJFEditNameViewController ()
@property (nonatomic, strong) GJFTextField *name;
@end

@implementation GJFEditNameViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //显示键盘
    [self.name becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.96 alpha:1.00];
    
    if (self.editPersonStyle == EditPersonStyleNickname) {
        self.navigationItem.title = @"昵称";
    }
    else {
        self.navigationItem.title = @"真实姓名";
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(popVC)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(saveName)];
    [self name];
}
- (void)popVC {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveName {
    if (self.name.textField.text.length == 0) {
        [self.view showWarning:@"请您输入信息"];
        return;
    }
    [self.view showBusyHUD];
    if (self.editPersonStyle == EditPersonStyleNickname) {
        
        NSDictionary *dic = @{@"nick_name": self.name.textField.text};
        [GJFNetworkTool POSTRequestWithUrl:APP_MINE_NICKNAME params:dic complete:^(NSDictionary *result) {
            [self.view hideBusyHUD];
            if ([result[@"code"] intValue] == 1000) {
                GJFUserInfo *userInfo = [GJFSingleTool shareInstance].userInfo;
                userInfo.nickname = self.name.textField.text;
                [GJFSingleTool saveUserInfo];
                [self popVC];
            }
        }];
    }
    else {
        NSDictionary *dic = @{@"real_name": self.name.textField.text};
        [GJFNetworkTool POSTRequestWithUrl:APP_MINE_REALNAME params:dic complete:^(NSDictionary *result) {
            [self.view hideBusyHUD];
            if ([result[@"code"] intValue] == 1000) {
                GJFUserInfo *userInfo = [GJFSingleTool shareInstance].userInfo;
                userInfo.realname = self.name.textField.text;
                [GJFSingleTool saveUserInfo];
                [self popVC];
            }
        }];
    }
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self.name resignFirstResponder];
//}

- (GJFTextField *)name {
    if (!_name) {
        _name = [[GJFTextField alloc] initWithFrame:CGRectMake(0, 20 * YY, WIDTH, 45 * YY)];
        _name.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _name.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_name];
        if (self.editPersonStyle == EditPersonStyleNickname) {
            _name.textField.placeholder = @"请输入您的昵称";
        }
        else {
            _name.textField.placeholder = @"请输入您的真实姓名";
        }
    }
    return _name;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
